﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SaturniLibrary.Web
{
   public class RobotsUtility
    {
       public static bool IsRobot(string userAgent)
       {
           return
               IsGoogleRobot(userAgent) ||
               IsBaiduRobot(userAgent) ||
               IsBingRobot(userAgent) ||
               Is360Robot(userAgent) ||
               IsSogouRobot(userAgent) ||
               IsSosoRobot(userAgent) ||
               IsYoudaoRobot(userAgent) ||
               IsYahooRobot(userAgent);
       }

       public static bool IsGoogleRobot(string userAgent)
       {
           if (string.IsNullOrWhiteSpace(userAgent))
           {
               return false;
           }
           return userAgent.IndexOf("Googlebot", StringComparison.OrdinalIgnoreCase) > 0;
       }

       public static bool IsBaiduRobot(string userAgent)
       {
           if (string.IsNullOrWhiteSpace(userAgent))
           {
               return false;
           }
           return userAgent.IndexOf("Baiduspider", StringComparison.OrdinalIgnoreCase) > 0;
       }

       public static bool IsBingRobot(string userAgent)
       {
           if (string.IsNullOrWhiteSpace(userAgent))
           {
               return false;
           }
           return userAgent.IndexOf("Bingbot", StringComparison.OrdinalIgnoreCase) > 0;
       }

       public static bool Is360Robot(string userAgent)
       {
           if (string.IsNullOrWhiteSpace(userAgent))
           {
               return false;
           }
           return userAgent.IndexOf("360Spider", StringComparison.OrdinalIgnoreCase) > 0;
       }

       public static bool IsSogouRobot(string userAgent)
       {
           if (string.IsNullOrWhiteSpace(userAgent))
           {
               return false;
           }
           return userAgent.IndexOf("sogou spider", StringComparison.OrdinalIgnoreCase) > 0 ||
               userAgent.IndexOf("sogouspider", StringComparison.OrdinalIgnoreCase) > 0;
       }

       public static bool IsSosoRobot(string userAgent)
       {
           if (string.IsNullOrWhiteSpace(userAgent))
           {
               return false;
           }
           return userAgent.IndexOf("Sosospider", StringComparison.OrdinalIgnoreCase) > 0;
       }

       public static bool IsYoudaoRobot(string userAgent)
       {
           if (string.IsNullOrWhiteSpace(userAgent))
           {
               return false;
           }
           return userAgent.IndexOf("YodaoBot", StringComparison.OrdinalIgnoreCase) > 0;
       }

       public static bool IsYahooRobot(string userAgent)
       {
           if (string.IsNullOrWhiteSpace(userAgent))
           {
               return false;
           }
           return userAgent.IndexOf("Yahoo! Slurp", StringComparison.OrdinalIgnoreCase) > 0 ||
               userAgent.IndexOf("YahooSeeker", StringComparison.OrdinalIgnoreCase) > 0;
       }
    }
}
